package com.raincat.tools.multimediapickertools;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.raincat.tools.R;
import com.raincat.tools.multimediapickertools.utils.EasyActionBar;
import com.raincat.tools.multimediapickertools.utils.MultiMediaController;
import com.raincat.tools.multimediapickertools.utils.MultiMediaHelper;
import com.raincat.tools.multimediapickertools.utils.MultiMediaItem;

import java.util.ArrayList;
import java.util.List;


/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JingYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2019/02/26
 *     desc   : 视频预览
 *     version: 1.0
 * </pre>
 */

public class VideoBrowseActivity extends AppCompatActivity {
    private Context context;

    private ViewPager viewPager;
    private EasyActionBar easyActionBar;
    private Button btn_sure;

    private List<MultiMediaItem> dataList = new ArrayList<>();
    private int checkIndex = -99;
    private int bucketIndex = -99;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
        localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        setTheme(MultiMediaController.themeId);
        setContentView(R.layout.picker_activity_videosbrowse);

        context = VideoBrowseActivity.this;

        dataList = (List<MultiMediaItem>) getIntent().getSerializableExtra("List");
        checkIndex = getIntent().getIntExtra("CheckIndex", 0);
        bucketIndex = getIntent().getIntExtra("BucketIndex", -99);

        if (bucketIndex != -99) {
            getData();
        } else {
            Init();
            setData();
        }
    }

    public void Init() {
        btn_sure = findViewById(R.id.btn_sure);
        viewPager = findViewById(R.id.viewpager);
        easyActionBar = findViewById(R.id.easyActionBar);
        easyActionBar.setTitleText((checkIndex + 1) + "/" + dataList.size());
        easyActionBar.setTitleRes(-1);
        easyActionBar.setImmerseLayout();

        if (MultiMediaController.multiMediaItemMap.get(dataList.get(checkIndex).path) == null)
            easyActionBar.setRightImageRes(false);
        else
            easyActionBar.setRightImageRes(true);
        listener();
    }

    private void getData() {
        dataList.addAll(MultiMediaHelper.getInstance(context).getVideosBucketList(context).multiMediaItemList);
        //移除相机item
        if (dataList.size() != 0 && dataList.get(0).path.equals(MultiMediaItem.CAMERA))
            dataList.remove(0);
        Init();
        setData();
    }

    private void setData() {
        PagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(checkIndex);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                checkIndex = viewPager.getCurrentItem();
                easyActionBar.setTitleText((checkIndex + 1) + "/" + dataList.size());
                if (MultiMediaController.multiMediaItemMap.get(dataList.get(checkIndex).path) == null)
                    easyActionBar.setRightImageRes(false);
                else
                    easyActionBar.setRightImageRes(true);
            }
        });
    }

    private void listener() {
        btn_sure.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                MultiMediaItem multiMediaItem = dataList.get(viewPager.getCurrentItem());
                MultiMediaController.multiMediaItemMap.clear();
                if (MultiMediaController.addOrRemoveMultiMediaItem(context, multiMediaItem)) {
                    Intent intent = new Intent();
                    intent.putExtra("finish", true);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    private class ViewPagerAdapter extends FragmentStatePagerAdapter {
        ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            VideoBrowseFragment fragment = new VideoBrowseFragment();
            fragment.setMultiMediaItem(dataList.get(position));
            return fragment;
        }

        @Override
        public int getCount() {
            return dataList.size();
        }
    }

    protected void onDestroy() {
        if (dataList != null && dataList.size() != 0)
            for (MultiMediaItem multiMediaItem : dataList)
                MultiMediaController.multiMediaItemMap.remove(multiMediaItem.path);
        Glide.get(this).clearMemory();
        System.gc();
        super.onDestroy();
    }
}
